{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ['CUDA_VISIBLE_DEVICES'] = ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tqdm import tqdm\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:root:Load quantized model will cause accuracy drop.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /home/husein/.local/lib/python3.6/site-packages/malaya/function/__init__.py:74: The name tf.gfile.GFile is deprecated. Please use tf.io.gfile.GFile instead.\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /home/husein/.local/lib/python3.6/site-packages/malaya/function/__init__.py:74: The name tf.gfile.GFile is deprecated. Please use tf.io.gfile.GFile instead.\n",
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /home/husein/.local/lib/python3.6/site-packages/malaya/function/__init__.py:76: The name tf.GraphDef is deprecated. Please use tf.compat.v1.GraphDef instead.\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /home/husein/.local/lib/python3.6/site-packages/malaya/function/__init__.py:76: The name tf.GraphDef is deprecated. Please use tf.compat.v1.GraphDef instead.\n",
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /home/husein/.local/lib/python3.6/site-packages/malaya/function/__init__.py:51: The name tf.ConfigProto is deprecated. Please use tf.compat.v1.ConfigProto instead.\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /home/husein/.local/lib/python3.6/site-packages/malaya/function/__init__.py:51: The name tf.ConfigProto is deprecated. Please use tf.compat.v1.ConfigProto instead.\n",
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /home/husein/.local/lib/python3.6/site-packages/malaya/function/__init__.py:66: The name tf.InteractiveSession is deprecated. Please use tf.compat.v1.InteractiveSession instead.\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /home/husein/.local/lib/python3.6/site-packages/malaya/function/__init__.py:66: The name tf.InteractiveSession is deprecated. Please use tf.compat.v1.InteractiveSession instead.\n",
      "\n",
      "WARNING:root:Load quantized model will cause accuracy drop.\n"
     ]
    }
   ],
   "source": [
    "import malaya\n",
    "\n",
    "model = malaya.dependency.transformer(model = 'xlnet', quantized = True)\n",
    "pos = malaya.pos.transformer(model = 'xlnet', quantized = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "t = 'Cerpen itu telah saya karang.'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "tokenizer = malaya.preprocessing.TOKENIZER().tokenize\n",
    "sastrawi = malaya.stem.sastrawi()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "graph, tagging, indexing = model.predict(' '.join(tokenizer(t)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.40.1 (20161225.0304)\n",
       " -->\n",
       "<!-- Title: G Pages: 1 -->\n",
       "<svg width=\"314pt\" height=\"305pt\"\n",
       " viewBox=\"0.00 0.00 313.50 305.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 301)\">\n",
       "<title>G</title>\n",
       "<polygon fill=\"#ffffff\" stroke=\"transparent\" points=\"-4,4 -4,-301 309.5,-301 309.5,4 -4,4\"/>\n",
       "<!-- 0 -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>0</title>\n",
       "<text text-anchor=\"middle\" x=\"158.5\" y=\"-275.3\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">0 (None)</text>\n",
       "</g>\n",
       "<!-- 5 -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>5</title>\n",
       "<text text-anchor=\"middle\" x=\"158.5\" y=\"-188.3\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">5 (karang)</text>\n",
       "</g>\n",
       "<!-- 0&#45;&gt;5 -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>0&#45;&gt;5</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M158.5,-260.9735C158.5,-249.1918 158.5,-233.5607 158.5,-220.1581\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"162.0001,-220.0033 158.5,-210.0034 155.0001,-220.0034 162.0001,-220.0033\"/>\n",
       "<text text-anchor=\"middle\" x=\"169.5\" y=\"-231.8\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">root</text>\n",
       "</g>\n",
       "<!-- 1 -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>1</title>\n",
       "<text text-anchor=\"middle\" x=\"37.5\" y=\"-101.3\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">1 (Cerpen)</text>\n",
       "</g>\n",
       "<!-- 5&#45;&gt;1 -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>5&#45;&gt;1</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M133.4287,-173.9735C115.258,-160.9086 90.5038,-143.1102 70.6932,-128.8662\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"72.7016,-125.9994 62.5392,-123.0034 68.6151,-131.6829 72.7016,-125.9994\"/>\n",
       "<text text-anchor=\"middle\" x=\"114.5\" y=\"-144.8\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">obl</text>\n",
       "</g>\n",
       "<!-- 2 -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>2</title>\n",
       "<text text-anchor=\"middle\" x=\"120.5\" y=\"-101.3\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">2 (itu)</text>\n",
       "</g>\n",
       "<!-- 5&#45;&gt;2 -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>5&#45;&gt;2</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M150.6264,-173.9735C145.3784,-161.9585 138.3819,-145.9401 132.4523,-132.3646\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"135.5737,-130.7664 128.3635,-123.0034 129.1589,-133.5683 135.5737,-130.7664\"/>\n",
       "<text text-anchor=\"middle\" x=\"151\" y=\"-144.8\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">det</text>\n",
       "</g>\n",
       "<!-- 3 -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>3</title>\n",
       "<text text-anchor=\"middle\" x=\"196.5\" y=\"-101.3\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">3 (telah)</text>\n",
       "</g>\n",
       "<!-- 5&#45;&gt;3 -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>5&#45;&gt;3</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M166.3736,-173.9735C171.6216,-161.9585 178.6181,-145.9401 184.5477,-132.3646\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"187.8411,-133.5683 188.6365,-123.0034 181.4263,-130.7664 187.8411,-133.5683\"/>\n",
       "<text text-anchor=\"middle\" x=\"203\" y=\"-144.8\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">advmod</text>\n",
       "</g>\n",
       "<!-- 4 -->\n",
       "<g id=\"node7\" class=\"node\">\n",
       "<title>4</title>\n",
       "<text text-anchor=\"middle\" x=\"275.5\" y=\"-101.3\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">4 (saya)</text>\n",
       "</g>\n",
       "<!-- 5&#45;&gt;4 -->\n",
       "<g id=\"edge6\" class=\"edge\">\n",
       "<title>5&#45;&gt;4</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M195.0514,-176.6596C206.6067,-170.9923 219.0725,-163.9779 229.5,-156 238.9256,-148.7887 247.9753,-139.4756 255.5167,-130.7851\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"258.2507,-132.9718 261.986,-123.0573 252.8831,-128.4785 258.2507,-132.9718\"/>\n",
       "<text text-anchor=\"middle\" x=\"259.5\" y=\"-144.8\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">nsubj</text>\n",
       "</g>\n",
       "<!-- 6 -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>6</title>\n",
       "<text text-anchor=\"middle\" x=\"37.5\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">6 (.)</text>\n",
       "</g>\n",
       "<!-- 1&#45;&gt;6 -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>1&#45;&gt;6</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M37.5,-86.9735C37.5,-75.1918 37.5,-59.5607 37.5,-46.1581\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"41.0001,-46.0033 37.5,-36.0034 34.0001,-46.0034 41.0001,-46.0033\"/>\n",
       "<text text-anchor=\"middle\" x=\"53\" y=\"-57.8\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">punct</text>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.files.Source at 0x7f8689570550>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "graph.to_graphvis()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('Cerpen', 'PROPN'),\n",
       " ('itu', 'DET'),\n",
       " ('telah', 'ADV'),\n",
       " ('saya', 'PRON'),\n",
       " ('karang', 'NOUN'),\n",
       " ('.', 'PUNCT')]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pos.predict(' '.join(tokenizer(t)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('Buku', 'NOUN'),\n",
       " ('itu', 'DET'),\n",
       " ('dibaca', 'VERB'),\n",
       " ('oleh', 'ADP'),\n",
       " ('Ahmad', 'PROPN'),\n",
       " ('.', 'PUNCT')]"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "malaya.stack.voting_stack([pos] * 3, ' '.join(tokenizer('Buku itu dibaca oleh Ahmad.')))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('Surat', 'NOUN'),\n",
       " ('itu', 'DET'),\n",
       " ('dihantar', 'VERB'),\n",
       " ('oleh', 'ADP'),\n",
       " ('abang', 'NOUN'),\n",
       " ('semalam', 'ADV'),\n",
       " ('.', 'PUNCT')]"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "malaya.stack.voting_stack([pos] * 3, ' '.join(tokenizer('Surat itu dihantar oleh abang semalam.')))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = malaya.stack.voting_stack([pos] * 3, ' '.join(tokenizer(t)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('PROPN', 'DET', 'ADV', 'PRON', 'NOUN', 'PUNCT')"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(zip(*r))[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "import copy\n",
    "import re\n",
    "from malaya.text.regex import _expressions\n",
    "\n",
    "def reset_t(tokens):\n",
    "    t = []\n",
    "    for i in range(len(tokens)):\n",
    "        t.append([tokens[i], 2])\n",
    "    return t\n",
    "\n",
    "def augment_12_0(t, row):\n",
    "    text, tokens, tokens_lower, tagging = row\n",
    "    for i in range(len(tokens) - 2):\n",
    "        if tagging[i] == 'ADV' \\\n",
    "        and tagging[i + 1] in ['PRON', 'NOUN'] \\\n",
    "        and tagging[i + 2] in ['VERB', 'NOUN'] \\\n",
    "        and tokens_lower[i] in ['telah', 'mesti']:\n",
    "            v = f'di{tokens[i + 2]}'\n",
    "            n = f'oleh {tokens[i + 1]}'\n",
    "            t[i][1] = 12\n",
    "            t[i + 1][0] = v\n",
    "            t[i + 1][1] = 12\n",
    "            t[i + 2][0] = n\n",
    "            t[i + 2][1] = 12\n",
    "            \n",
    "def augment_12_1(t, row):\n",
    "    text, tokens, tokens_lower, tagging = row\n",
    "    for i in range(len(tokens) - 1):\n",
    "        if tagging[i] == 'PRON' and tagging[i + 1] == 'VERB' and sastrawi.stem(tokens[i + 1]) == tokens[i + 1]:\n",
    "            v = f'men{tokens[i + 1]}'\n",
    "            if sastrawi.stem(v) == v:\n",
    "                v = f'mem{tokens[i + 1]}'\n",
    "            t[i][1] = 12\n",
    "            t[i + 1][0] = v\n",
    "            t[i + 1][1] = 12\n",
    "            \n",
    "def augment_12_2(t, row):\n",
    "    text, tokens, tokens_lower, tagging = row\n",
    "    for i in range(len(tokens) - 2):\n",
    "        if tagging[i] == 'VERB' \\\n",
    "        and tagging[i + 1] in ['ADP'] \\\n",
    "        and tagging[i + 2] in ['PRON', 'NOUN'] \\\n",
    "        and tokens_lower[i + 1] in ['oleh']:\n",
    "            v = sastrawi.stem(tokens[i])\n",
    "            t[i][0] = tokens[i + 2]\n",
    "            t[i][1] = 12\n",
    "            t[i + 1][0] = v\n",
    "            t[i + 1][1] = 12\n",
    "            t[i + 2][0] = ''\n",
    "            t[i + 2][1] = 12\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 4/4 [00:00<00:00,  4.90it/s]\n"
     ]
    }
   ],
   "source": [
    "results = []\n",
    "for text in tqdm(['Cerpen itu telah saya karang.', \n",
    "                  'Latihan itu mesti kau buat.',\n",
    "                  'Kereta itu saya beli daripada Ali.',\n",
    "                  'Surat itu dihantar oleh abang semalam.']):\n",
    "    tokens = tokenizer(text)\n",
    "    t = reset_t(tokens)\n",
    "    t_ = copy.deepcopy(t)\n",
    "    tokens_lower = tokenizer(text.lower())\n",
    "    tagging = malaya.stack.voting_stack([pos] * 3, ' '.join(tokens))\n",
    "    tagging = list(zip(*tagging))[1]\n",
    "    r = (t, tokens, tokens_lower, tagging)\n",
    "    augment_12_0(t_, r)\n",
    "    a = list(zip(*t_))[1]\n",
    "    if 12 not in a:\n",
    "        augment_12_1(t_, r)\n",
    "    a = list(zip(*t_))[1]\n",
    "    if 12 not in a:\n",
    "        augment_12_2(t_, r)\n",
    "    results.append((t, t_))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[([['Cerpen', 2],\n",
       "   ['itu', 2],\n",
       "   ['telah', 2],\n",
       "   ['saya', 2],\n",
       "   ['karang', 2],\n",
       "   ['.', 2]],\n",
       "  [['Cerpen', 2],\n",
       "   ['itu', 2],\n",
       "   ['telah', 12],\n",
       "   ['dikarang', 12],\n",
       "   ['oleh saya', 12],\n",
       "   ['.', 2]]),\n",
       " ([['Latihan', 2],\n",
       "   ['itu', 2],\n",
       "   ['mesti', 2],\n",
       "   ['kau', 2],\n",
       "   ['buat', 2],\n",
       "   ['.', 2]],\n",
       "  [['Latihan', 2],\n",
       "   ['itu', 2],\n",
       "   ['mesti', 12],\n",
       "   ['dibuat', 12],\n",
       "   ['oleh kau', 12],\n",
       "   ['.', 2]]),\n",
       " ([['Kereta', 2],\n",
       "   ['itu', 2],\n",
       "   ['saya', 2],\n",
       "   ['beli', 2],\n",
       "   ['daripada', 2],\n",
       "   ['Ali', 2],\n",
       "   ['.', 2]],\n",
       "  [['Kereta', 2],\n",
       "   ['itu', 2],\n",
       "   ['saya', 12],\n",
       "   ['membeli', 12],\n",
       "   ['daripada', 2],\n",
       "   ['Ali', 2],\n",
       "   ['.', 2]]),\n",
       " ([['Surat', 2],\n",
       "   ['itu', 2],\n",
       "   ['dihantar', 2],\n",
       "   ['oleh', 2],\n",
       "   ['abang', 2],\n",
       "   ['semalam', 2],\n",
       "   ['.', 2]],\n",
       "  [['Surat', 2],\n",
       "   ['itu', 2],\n",
       "   ['abang', 12],\n",
       "   ['hantar', 12],\n",
       "   ['', 12],\n",
       "   ['semalam', 2],\n",
       "   ['.', 2]])]"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
